<template>
	<view class="content">
		<view class="address">
			<view class="address_cont">
				<view class="address_icon">
					<image src="https://taobaoke-paimai.oss-cn-beijing.aliyuncs.com/addr.png" mode=""></image>
				</view>
				<view class="address_detail" v-if="yxUserAddress == null" @click="selectAddress">添加收货地址</view>
				<view class="address_detail" v-else @click="selectAddress">
					<view class="address_name">
						<view class="name">{{yxUserAddress.realName}}</view>
						<view class="phone">{{yxUserAddress.phone}}</view>
					</view>
					<view class="address_info">{{yxUserAddress.province}}{{yxUserAddress.city}}{{yxUserAddress.district}}{{yxUserAddress.detail}}</view>
				</view>
				<view class="iconfont select_icon">&#xe601;</view>
			</view>
			
		</view>
		<view class="group">
			<view class="goods" v-for="(item,index) in orderDatas.cartInfo">
				<view class="goods_image">
					<image :src="item.productInfo.image" mode=""></image>
				</view>
				<view class="goods_cont">
					<view class="goods_name ellipsis_2">{{item.productInfo.storeName}}</view>
					<view class="goods_bot">
						<view class="goods_price">
							<view class="sign iconfont">&#xe604;</view>
							<view class="price">
								<text > {{item.productInfo.attrInfo.integral}}积分</text>
							</view>
						</view>
						<view class="goods_num">X{{item.cartNum}}</view>
					</view>
				</view>
			</view>
			<view class="freight">
				<view class="freight_fl">运费</view>
				<view class="freight_fr">包邮</view>
			</view>
		</view>
		
		<view class="bottom">
			<view class="bottom_total">合计：
				<!-- <text class="iconfont">&#xe604;</text> {{totalPrice}} &nbsp; -->
				<text> {{totalIntegral}}积分</text>
			</view>
			<view class="submit" @click="sureOrder">提交订单</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderDatas:{},
				yxUserAddress:{},
				userAddressId:'',
				cartId:''  ,// 购物车id
				totalPrice:'',
				orderKey:''  ,// 订单key值
				totalIntegral:''
 			}
		},
		onLoad(options) {
			console.log(options)
			this.cartId = options.cartId;
			this.getOrderInfo()
			let that = this;
			uni.$on('updateAddress',function(data){
				console.log('监听到事件来自 id ，携带参数 msg 为：' + data.msg);
				that.getUserAddress();
			})
		},
		onUnload() {
			uni.$off('updateAddress');
		},
		methods: {
			getOrderInfo: function () {
				this.api.integralSureOrder({
					cartId:this.cartId
				}).then(res => {
					console.log(res)
					this.orderDatas = res.data;
					this.orderKey = res.data.orderKey;
					this.totalPrice = res.data.priceGroup.totalPrice
					this.totalIntegral = res.data.priceGroup.totalIntegral;
					if (res.data.addressInfo != null) {
						this.yxUserAddress = res.data.addressInfo;
						this.userAddressId = res.data.addressInfo.id;
					} else {
						this.yxUserAddress = null
					}
				})
			},
			// 选择地址
			selectAddress: function () {
				uni.navigateTo({
					url: '../../user/address/address?addressType=1',
				});
			},
			// 根据地址id获取地址
			getUserAddress: function () {
				let that = this;
				that.api.getUserAddressDetail(that.userAddressId).then(res => {
					console.log(res)
					that.yxUserAddress = res.data;
				})
			},
			// 提交订单
			sureOrder: function () {
				let that = this;
				if (that.userAddressId == '') {
					that.tui.toast('请选择收货地址')
					return
				}
				that.api.userCreationOrder(that.orderKey,{
					mark:'',
					addressId:that.userAddressId
				}).then(res => {
					console.log(res)
					that.tui.toast(res.msg);
					let orderPay = JSON.stringify(res.data.result);
					// let orderInfo = JSON.stringify(orderPay)
					console.log(orderPay)
					setTimeout(function () {
						uni.$emit('goodsOrder',{msg:'页面更新'});
						uni.$emit('shoppingCar',{msg:'页面更新'});
					},500)
					setTimeout(function () {
						uni.redirectTo({
							url: '../paymentOrder/paymentOrder?orderInfo=' + orderPay,
						});
					},1500)
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		width: 100%;
		box-sizing: border-box;
		letter-spacing: 3rpx;
		font-family: PingFang SC;
		color: #140005;
		font-size: 28rpx;
		padding: 20rpx;
	}
	.content {
		width: 100%;
		box-sizing: border-box;
	}
	.address {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		padding: 30rpx 22rpx;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		.address_cont {
			width: 100%;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #666666;
			.address_icon {
				width: 28rpx;
				height: 28rpx;
				image {
					width: 100%;
					height: 100%;
				}
			}
			.select_icon {
				width: 30rpx;
				box-sizing: border-box;
				text-align: center;
				font-size: 30rpx;
				color: #999999;
			}
			.address_detail {
				width: calc(100% - 60rpx);
				box-sizing: border-box;
				padding: 15rpx 0 15rpx 20rpx;
				.address_name {
					width: 100%;
					box-sizing: border-box;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					line-height: 60rpx;
					.name {
						padding-right: 30rpx;
					}
				}
				.address_info {
					width: 100%;
					box-sizing: border-box;
					// padding-top: 15rpx;
				}
			}
			
		}
	}
	.group {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		padding: 0 22rpx;
		border-radius: 12rpx;
		.goods {
			width: 100%;
			box-sizing: border-box;
			padding: 30rpx 0;
			display: flex;
			justify-content: space-between;
			border-bottom: 2rpx solid #F5F5F5;
			
			.goods_image {
				width: 160rpx;
				height: 160rpx;
				image {
					width: 100%;
					height: 100%;
					border-radius: 16rpx;
				}
			}
			.goods_cont {
				width: calc(100% - 160rpx);
				box-sizing: border-box;
				padding-left: 20rpx;
				display: flex;
				flex-flow: column;
				justify-content: space-between;
				.goods_bot {
					width: 100%;
					box-sizing: border-box;
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 22rpx;
					color: #212121;
					.goods_price {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						color: #E42221;
						.price {
							font-size: 28rpx;
						}
					}
				}
				.goods_name {
					width: 100%;
					box-sizing: border-box;
					line-height: 40rpx;
				}
			}
		}
		.freight {
			width: 100%;
			box-sizing: border-box;
			line-height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	.bottom {
		width: 100%;
		box-sizing: border-box;
		background: #FFFFFF;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		.submit {
			width: 236rpx;
			height: 100rpx;
			box-sizing: border-box;
			text-align: center;
			font-size: 36rpx;
			line-height: 100rpx;
			color: #FFFFFF;
			background: #E42221;
		}
		.bottom_total {
			width: calc(100% - 236rpx);
			box-sizing: border-box;
			padding-right: 53rpx;
			font-size: 28rpx;
			color: #E42221;
			text-align: right;
			font-weight: 500;
		}
	}

</style>
